package obecto.task
{
	import flash.events.EventDispatcher;
	
	import obecto.event.TaskEvent;
	import obecto.event.TaskEventKind;
	
	[Event(name="start", type="obecto.event.TaskEvent")]
	[Event(name="complete", type="obecto.event.TaskEvent")]
	
	public class ExecuteMethodTask extends EventDispatcher implements ITask
	{
		private var _method : Function;
		public function set method(value : Function) : void
		{
			_method = value;
		}
		public function get method() : Function
		{
			return _method;
		}
		
		private var _arguments : Array;
		public function set arguments(value : Array) : void
		{
			_arguments = value;	
		}
		public function get arguments() : Array
		{
			return _arguments;
		}
		
		private var _running : Boolean = false;
		public function get running() : Boolean
		{
			return _running;
		}
		
		private var _methodScope : Object;
		//Check do we need method scope in AS3.
		public function ExecuteMethodTask(methodScope : Object, method : Function, arguments : Array)
		{	
			_method = method;
			_arguments = arguments;
			_methodScope = methodScope;
		}
		
		public function start() : void
		{
			_running = true;
			dispatchEvent(new TaskEvent(TaskEvent.START, TaskEventKind.NONE));
			
			method.apply(_methodScope, arguments);
			
			_running = false;
			dispatchEvent(new TaskEvent(TaskEvent.COMPLETE, TaskEventKind.END));
		}
	}
}